package user.zc.config.user.config;

import lombok.Getter;
import lombok.ToString;
import user.zc.config.IConfig;
import user.zc.config.ValueType;
import user.zc.config.system.config.SystemConfig;
import user.zc.config.system.config.SystemOutputFileConfig;
import user.zc.config.value.ColumnValue;
import user.zc.config.value.Key;
import user.zc.config.value.KeyValue;
import user.zc.util.ConfigGroupHelper;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhouchuang
 * @date 2023/8/9 17:55
 */
@Getter
@ToString
public class ConnectionConfig extends UserConfig<KeyValue> {
    private List<String> systemfold = new ArrayList<String>(){{add("system");add("template");}};

    private String url ;
    private String user ;
    private String password ;

    public ConnectionConfig() {
        super(ValueType.KeyValue);
    }

    @Override
    public void loadConfig(String fileName) throws Exception{
        data = readEntity(fileName, KeyValue.class);
        this.url = data.get("url").getValue();
        this.user = data.get("user").getValue();
        this.password = data.get("password").getValue();
    }

    public  Map<String, KeyValue> read(String fileName) throws Exception {
        return readEntity(fileName, KeyValue.class);
    }

    public List<String> list()throws Exception{
        return filesByType(ConnectionConfig.class).stream().map(name->name.split("_")[0]).collect(Collectors.toList());
    }

    private List<String> filesByType(Class<?> clazz){
        //除掉system和模板，其他都是
        File file =  new File(System.getProperty("user.home")+File.separator+ IConfig.FOLD);
        return Arrays.asList(file.list()).stream().filter(name->!systemfold.contains(name)).collect(Collectors.toList());
    }
}
